﻿//-----------------------------------------------------------------------
// <copyright file="ProcessDataContractUnitTest.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.Diagnostics.Contracts
{
    using System;
    using System.Diagnostics;
    
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Provides unit tests for the <see cref="T:CodePlex.Diagnostics.Contracts.ProcessDataContract"/> class.
    /// </summary>
    [TestClass]
    public sealed class ProcessDataContractUnitTest
    {
        #region public ProcessDataContractUnitTest()

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ProcessDataContractUnitTest"/> class.
        /// </summary>
        public ProcessDataContractUnitTest()
        {
        }

        #endregion

        #region public TestContext TestContext

        /// <summary>
        /// Gets or sets the test context which provides information about and functionality for the current test run.
        /// </summary>
        public TestContext TestContext
        {
            get;
            set;
        }

        #endregion

        #region public void IIdentityDataContractConstructorUnitTest()

        /// <summary>
        /// Ensures that the constructor of the <see cref="T:CodePlex.Diagnostics.Contracts.ProcessDataContract"/> class is correctly copying the values from the supplied <see cref="T:System.Diagnostics.Process"/> instance.
        /// </summary>
        [TestMethod]
        public void IIdentityDataContractConstructorUnitTest()
        {
            Process currentProcess = Process.GetCurrentProcess();
            
            ProcessDataContract process = new ProcessDataContract(currentProcess);

            Assert.AreEqual<int>(process.BasePriority, currentProcess.BasePriority, "data contract contains an invalid base priority value.");

            if (process.HasExited)
            {
                Assert.AreEqual<int>(process.ExitCode, currentProcess.ExitCode, "data contract contains an invalid exit code value.");

                Assert.AreEqual<DateTime>(process.ExitTime, currentProcess.ExitTime, "data contract contains an invalid exit time value.");
            }
            
            Assert.AreEqual<int>(process.HandleCount, currentProcess.HandleCount, "data contract contains an invalid handle count value.");

            Assert.AreEqual<bool>(process.HasExited, currentProcess.HasExited, "data contract contains an invalid value indicating whether the process has exited or not.");

            Assert.AreEqual<int>(process.Id, currentProcess.Id, "data contract contains an invalid Id value.");

            // ensure the correct machine name is used within the unit test.
            if (string.Equals(currentProcess.MachineName, "."))
            {
                // process appears to be executing upon the local machine and therefore the environment machine name will be used instead.
                Assert.AreEqual<string>(process.MachineName, Environment.MachineName, "data contract contains an invalid machine name value.");
            }
            else
            {
                // process appears to be executing upon another machine and therefore use the process machine name.
                Assert.AreEqual<string>(process.MachineName, currentProcess.MachineName, "data contract contains an invalid machine name value.");
            }

            Assert.AreEqual<string>(process.MainWindowTitle, currentProcess.MainWindowTitle, "data contract contains an invalid main window title value.");

            Assert.AreEqual<IntPtr>(process.MaxWorkingSet, currentProcess.MaxWorkingSet, "data contract contains an invalid maximum working set value.");

            Assert.AreEqual<IntPtr>(process.MinWorkingSet, currentProcess.MinWorkingSet, "data contract contains an invalid minimum working set value.");

            Assert.AreEqual<long>(process.NonpagedSystemMemorySize64, currentProcess.NonpagedSystemMemorySize64, "data contract contains an invalid non-paged system memory size value.");

            Assert.AreEqual<long>(process.PagedMemorySize64, currentProcess.PagedMemorySize64, "data contract contains an invalid paged memory size value.");

            Assert.AreEqual<long>(process.PagedSystemMemorySize64, currentProcess.PagedSystemMemorySize64, "data contract contains an invalid paged system memory size value.");

            Assert.AreEqual<long>(process.PeakPagedMemorySize64, currentProcess.PeakPagedMemorySize64, "data contract contains an invalid peak paged memory size value.");

            Assert.AreEqual<long>(process.PeakVirtualMemorySize64, currentProcess.PeakVirtualMemorySize64, "data contract contains an invalid peak virtual memory size value.");

            Assert.AreEqual<long>(process.PeakWorkingSet64, currentProcess.PeakWorkingSet64, "data contract contains an invalid peak working set value.");

            Assert.AreEqual<bool>(process.PriorityBoostEnabled, currentProcess.PriorityBoostEnabled, "data contract contains an invalid value indicating whether priority boost is enabled or not.");

            Assert.AreEqual<string>(process.PriorityClass, currentProcess.PriorityClass.ToString(), "data contract contains an invalid peak working set value.");

            Assert.AreEqual<long>(process.PrivateMemorySize64, currentProcess.PrivateMemorySize64, "data contract contains an invalid private memory size value.");

            Assert.AreEqual<TimeSpan>(process.PrivilegedProcessorTime, currentProcess.PrivilegedProcessorTime, "data contract contains an invalid privileged processor time value.");
            
            Assert.AreEqual<string>(process.ProcessName, currentProcess.ProcessName, "data contract contains an invalid process name value.");

            Assert.AreEqual<IntPtr>(process.ProcessorAffinity, currentProcess.ProcessorAffinity, "data contract contains an invalid processor affinity value.");

            Assert.AreEqual<bool>(process.Responding, currentProcess.Responding, "data contract contains an invalid value indicating whether the process is responding or not.");

            Assert.AreEqual<int>(process.SessionId, currentProcess.SessionId, "data contract contains an invalid session Id value.");

            Assert.AreEqual<DateTime>(process.StartTime, currentProcess.StartTime, "data contract contains an invalid start time value.");

            Assert.AreEqual<int>(process.Threads, currentProcess.Threads.Count, "data contract contains an invalid start time value.");

            Assert.IsTrue(process.TotalProcessorTime <= currentProcess.TotalProcessorTime, "data contract contains an invalid total processor time value.");

            Assert.IsTrue(process.UserProcessorTime <= currentProcess.UserProcessorTime, "data contract contains an invalid user processor time value.");

            Assert.AreEqual<long>(process.VirtualMemorySize64, currentProcess.VirtualMemorySize64, "data contract contains an invalid virtual memory size value.");

            Assert.AreEqual<long>(process.WorkingSet64, currentProcess.WorkingSet64, "data contract contains an invalid working set value.");
        }

        #endregion
    }
}
